package com.xnyzc.lhy.mis.controller.cmsapi.approval;


import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.param.approval.ApprovalTempInfoParam;
import com.xnyzc.lhy.mis.service.approval.IOaApprovalTempPrimaryService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 审批流模板主表 前端控制器
 * </p>
 *
 * @author huxuekuo
 * @since 2019-08-07
 */
@Slf4j
@RestController
@RequestMapping("/cmsapi/approval")
public class OaApprovalTempPrimaryController {


    @Autowired
    private IOaApprovalTempPrimaryService iOaApprovalTempPrimaryService;


    /**
     * 获取审批流模板列表
     *
     * @return
     */
    @GetMapping(value = "/getCmsApprovalTempList")
    public Rv getCmsApprovalTempList(String companyId,String companyNameSearch,String approvalNameSearch,Integer page) {
        String tag = TokenUtil.getTag();

        return iOaApprovalTempPrimaryService.getCmsApprovalTempList(companyNameSearch, approvalNameSearch, page, tag);
    }

    /**
     * 获取公司下的部门
     *
     * @param companyId
     * @return
     */
    @GetMapping(value = "/getCmsLinkOfficeList")
    public Rv getCmsLinkOfficeList(Long companyId) {
        if (CheckUtil.objIsEmpty(companyId)) {
            throw new PangException(EErrorCode.missingArg);
        }
        return iOaApprovalTempPrimaryService.getCmsLinkOfficeList(companyId);
    }

    /**
     * 保存部门的关联
     *
     * @param data
     * @return
     */
    @PostMapping(value = "/saveLinkOffice")
    public Rv saveLinkOffice(@RequestParam(value = "data") String data) {

        JSONObject jsonObject = JSONObject.fromObject(data);
        Long companyId = jsonObject.getLong("companyId");
        String checkNodes = jsonObject.getString("checkNodes");
        Long approvalId = jsonObject.getLong("approvalId");

        return iOaApprovalTempPrimaryService.saveLinkOffice(companyId, checkNodes, approvalId);
    }

    /**
     * 获取当前登录人的公司列表
     *
     * @return
     */
    @GetMapping(value = "/getCmsUserCompanyList")
    public Rv getCmsUserCompanyList() {

        return iOaApprovalTempPrimaryService.getCmsUserCompanyList();
    }

    /**
     * 获取公司下的成员列表
     *
     * @return
     */
    @GetMapping(value = "/getSpecialUserList")
    public Rv getSpecialUserList(Long companyCode) {
        if (CheckUtil.objIsEmpty(companyCode)) {
            throw new PangException(EErrorCode.missingArg);
        }

        return iOaApprovalTempPrimaryService.getSpecialUserList(companyCode);
    }

    /**
     * 保存审批节点信息
     *
     * @param approvalTempInfoParam
     * @return
     */
    @PostMapping(value = "/saveApprovalTempInfo")
    public Rv saveApprovalTempInfo(@RequestBody ApprovalTempInfoParam approvalTempInfoParam) {

        if (CheckUtil.objIsEmpty(approvalTempInfoParam)) {
            throw new PangException(EErrorCode.missingArg);
        }

        return iOaApprovalTempPrimaryService.saveApprovalTempInfo(approvalTempInfoParam);
    }

    /**
     * 获取审批流详情
     *
     * @return
     */
    @GetMapping(value = "/getOaApprovalPrimaryInfo")
    public Rv getOaApprovalPrimaryInfo(Long approvalId) {
        if (CheckUtil.objIsEmpty(approvalId)) {
            throw new PangException(EErrorCode.missingArg);
        }

        return iOaApprovalTempPrimaryService.getOaApprovalPrimaryInfo(approvalId);
    }

}
